package com.tencent.wxcloudrun.controller.fangdong;

import cn.hutool.core.collection.CollUtil;
import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.config.UserContext;
import com.tencent.wxcloudrun.pojo.bo.HouseStatusEnum;
import com.tencent.wxcloudrun.pojo.dto.req.AddHouseReq;
import com.tencent.wxcloudrun.pojo.dto.req.UpdateHouseReq;
import com.tencent.wxcloudrun.pojo.dto.resp.GetFuYongResp;
import com.tencent.wxcloudrun.pojo.dto.resp.GetHomeListResp;
import com.tencent.wxcloudrun.pojo.dto.resp.UdIdToListResp;
import com.tencent.wxcloudrun.service.business.fangdong.HouseManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 房屋管理
 */
@RestController
@RequestMapping("/houseManage")
@Validated
@Slf4j
public class HouseManageController {

    @Resource
    private HouseManageService houseManageService;

    /**
     * 房屋标签的总枚举值
     */
    @GetMapping("/getHouseTag")
    public ApiResponse<List<String>> getHouseTag() {
        return new ApiResponse<>(houseManageService.getHouseTag());
    }

    /**
     * 新增房屋信息
     */
    @PostMapping("/addHouse")
    public ApiResponse<String> addHouse(@Valid @RequestBody AddHouseReq req) {
        return new ApiResponse<>(houseManageService.addHouse(req));
    }

    /**
     * 修改房屋信息
     */
    @PostMapping("/updateHouse")
    public ApiResponse<String> updateHouse(@Valid @RequestBody UpdateHouseReq req) {
        return new ApiResponse<>(houseManageService.updateHouse(req));
    }

    /**
     * 注销房东身份
     */
    @GetMapping("/logOff")
    public ApiResponse<String> logOff() {
        return new ApiResponse<>(houseManageService.logOff());
    }

    /**
     * 获取房屋列表页面
     */
    @GetMapping("/getHomeList")
    public ApiResponse<List<GetHomeListResp>> getHomeList() {
        return new ApiResponse<>(houseManageService.getHomeList());
    }

    /**
     * 获取房屋状态的下拉列表
     */
    @GetMapping("/getHouseSelect")
    public ApiResponse<List<String>> getHouseSelect() {
        return new ApiResponse<>(CollUtil.newArrayList(HouseStatusEnum.NO_RENT.getKey(),
                HouseStatusEnum.WAIT_RENT.getKey(), HouseStatusEnum.RENT.getKey()));
    }

    /**
     * 修改某房屋的状态
     */
    @GetMapping("/updateHouseStatus")
    public ApiResponse<String> updateHouseStatus(@RequestParam("houseId") String houseId, @RequestParam("status") String status) {
        return new ApiResponse<>(houseManageService.updateHouseStatus(houseId, status));
    }

    /**
     * 获取复用列表
     */
    @GetMapping("/getFuYong")
    public ApiResponse<GetFuYongResp> getFuYong() {
        return new ApiResponse<>(houseManageService.getFuYong());
    }

    /**
     * 检查房屋认证状态，用于二维码生成前的验证
     */
    @PostMapping("/checkHouseAuth")
    public ApiResponse<String> checkHouseAuth(@RequestBody List<String> ids) {
        return new ApiResponse<>(houseManageService.checkHouseAuth(ids));
    }

    /**
     * 勾选房屋信息后给前端展示二维码
     */
    @PostMapping("/getQrCodeId")
    public ResponseEntity<byte[]> getQrCodeId(@RequestBody List<String> ids, HttpServletResponse response) throws IOException {
        String openId = UserContext.getOpenId();
        byte[] qrCode = houseManageService.getQrCodeId(openId, ids, response);
        response.setContentType("image/png");
        response.setHeader("Content-Disposition", "inline; filename=qrCode.png");
        return ResponseEntity.ok().header("Content-Type", "application/octet-stream").body(qrCode);
    }

    /**
     * 使用ID获取房屋信息
     *
     * @param id 生成二维码时会对应的生成一个与这个二维码对应的唯一性编码来着，这个入参就是那个编码
     */
    @GetMapping("/getHomeInfoById")
    public ApiResponse<List<UdIdToListResp>> getHomeInfoById(@RequestParam("id") String id) {
        return new ApiResponse<>(houseManageService.getHomeInfoById(id));
    }

    /**
     * 检查房屋认证状态，用于二维码生成前的验证
     */
    @PostMapping("/checkHouseAuthIng")
    public ApiResponse<String> checkHouseAuthIng(@RequestBody List<String> ids) {
        return new ApiResponse<>(houseManageService.checkHouseAuthIng(ids));
    }

    /**
     * 发起房屋认证申请
     */
    @PostMapping("/applyHouseAuth")
    public ApiResponse<String> applyHouseAuth(@RequestBody List<String> ids) {
        return new ApiResponse<>(houseManageService.applyHouseAuth(ids));
    }

}
